package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

@MessageTag(value = IMMessageType.SYSTEM_PUBLISH ,flag = MessageTag.NONE)
public class RXSystemPublishContent extends MessageContent {

    private String data;
    private boolean destruct;
    private long destructTime;
    private String jSONDestructInfo;
    private int type;

    @Override
    public byte[] encode() {
        JSONObject object =new JSONObject();
        object.put("data", data);
        object.put("type", type);
        object.put("destruct", destruct);
        object.put("destructTime",destructTime);
        object.put("jSONDestructInfo",jSONDestructInfo);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RXSystemPublishContent() {
    }

    public RXSystemPublishContent(byte[] data) {
        super(data);
        String jsonStr=null;
        try {
            jsonStr =new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);

            setData(object.getString("data"));
            setType(object.getIntValue("type"));
            setDestruct(object.getBoolean("destruct"));
            setDestructTime(object.getLong("destructTime"));
            setjSONDestructInfo(object.getString("jSONDestructInfo"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, data);
        ParcelUtils.writeToParcel(dest, jSONDestructInfo);
    }

    public static final Creator<RXSystemPublishContent> CREATOR = new Creator<RXSystemPublishContent>() {

        @Override
        public RXSystemPublishContent createFromParcel(Parcel source) {
            return new RXSystemPublishContent(source);
        }

        @Override
        public RXSystemPublishContent[] newArray(int size) {
            return new RXSystemPublishContent[size];
        }
    };


    public RXSystemPublishContent(Parcel parcel){
        data = ParcelUtils.readFromParcel(parcel);
        jSONDestructInfo = ParcelUtils.readFromParcel(parcel);
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public boolean isDestruct() {
        return destruct;
    }

    @Override
    public void setDestruct(boolean destruct) {
        this.destruct = destruct;
    }

    @Override
    public long getDestructTime() {
        return destructTime;
    }

    @Override
    public void setDestructTime(long destructTime) {
        this.destructTime = destructTime;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getjSONDestructInfo() {
        return jSONDestructInfo;
    }

    public void setjSONDestructInfo(String jSONDestructInfo) {
        this.jSONDestructInfo = jSONDestructInfo;
    }
}

